﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ticTacToe.Models;
using ticTacToe.Domain;
namespace ticTacToe.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        //renders back the one player selection
        public ActionResult Index()
        {
            return View();
        }

        //starts and sets up a new game
        public ActionResult Play(String player1, String player2, String gametype, String diff)
        {
            GameModel model = getGame(player1, player2, gametype, diff);
            ViewData["Model"] = model;
            return View("Play", model);
        }

        //makes a mark on a given board
        public ActionResult Check(int square, char mark, String gameId)
        {
            //get the players game back from application cache
            TttGame tGame = (TttGame)this.HttpContext.Application[gameId];
            tGame.play(square);
            this.HttpContext.Application[tGame.getGameId()] = tGame;
            GameModel rslt = gameToModel(tGame);
            return Json(rslt) ;
        }

        public ActionResult restart(String gameId, String player1, String player2, String gametype, String diff)
        {

            GameModel model = getGame(player1, player2, gametype, diff);
            ViewData["Model"] = model;
            return View("Play", model);
       }

        private GameModel getGame(String player1, String player2, String gametype, String diff)
        {
            TttGame tGame;
            UserModel u1 = new UserModel(player1);
            UserModel u2 = new UserModel(player2);
            char mark = 'X';
            if (gametype == "One player game") {        //starts a 1 player game against the computer
               tGame  = new TttGame(u1, mark, 1);
            }
            else{                                       //starts a 2
                tGame = new TttGame(u1, mark, u2);
            }
            this.HttpContext.Application[tGame.getGameId()] = tGame;
            GameModel model = gameToModel(tGame);
            if (mark == 'X')
            {
                model.player1 = tGame.playerX.getUsername();
                model.player1Mark = mark;
                model.player2 = tGame.playerO.getUsername();
                model.player2Mark = tGame.playerO.getMark();
            }
            else {
                model.player1 = tGame.playerO.getUsername();
                model.player1Mark = mark;
                model.player2 = tGame.playerX.getUsername();
                model.player2Mark = tGame.playerX.getMark();
            }
            model.gameType = gametype;
            model.difficulty = diff;

            return model;
    
        }

        private GameModel gameToModel(TttGame g) {
            GameModel model = new GameModel();
            model.gameId = g.getGameId();
            model.board = g.gameboard.getBoard();
            model.moveCount = g.gameboard.getPlayCount();
            model.nextMove = g.gameboard.getNextPlayBelongsTo();
            String state = g.validateBoard();
            String wr = state.Substring(state.Length-5,4);
            if (state == "Error: innvalid game board") {
                model.message = "Error";
                model.winningRow = null;
                return model;
            }
            else if(state == "Game was a draw"){
                model.message = state;
                model.winningRow = "no";
                return model;
            }
            else if (wr == ";row" || wr == ";col" || wr == ";dia")
            {
                model.message = "Winner";
                model.winningRow = state;
                return model;
            }
            else {
                model.message = model.nextMove.ToString();
                model.winningRow = null;
            }

            return model;

            
        }
       
    }
}
